package com.example.myweather.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView

import com.example.myweather.R
import com.example.myweather.bean.WeatherApi
import com.example.myweather.tools.Weather_Dict
import com.socks.library.KLog

import java.util.Calendar
import java.util.TimeZone

import com.example.myweather.tools.Weather_Dict.switchweek

class Hour_skycon_tem_adapter(private val mweatherApi: WeatherApi) : RecyclerView.Adapter<Hour_skycon_tem_adapter.ViewHolder>() {

    private var mContext: Context? = null

    internal class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var cardView: CardView
        var hour: TextView
        var tem: TextView
        var skycon: ImageView

        init {
            cardView = view as CardView
            hour = itemView.findViewById<View>(R.id.result_hour_skycon_tem_hour) as TextView
            skycon = itemView.findViewById<View>(R.id.result_hour_skycon_tem_skycon) as ImageView
            tem = itemView.findViewById<View>(R.id.result_hour_skycon_tem_tem) as TextView
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Hour_skycon_tem_adapter.ViewHolder {
        if (mContext == null) {
            mContext = parent.context
        }
        val view = LayoutInflater.from(parent.context).inflate(R.layout.card_view_hour, parent, false)
        val holder = Hour_skycon_tem_adapter.ViewHolder(view)
        holder.cardView.setOnClickListener {
            val position = holder.adapterPosition
            Toast.makeText(mContext, position.toString(), Toast.LENGTH_SHORT).show()
            //                    Count count=mCountList.get(position);
            //                Intent intent=new Intent(mContext,Exam2tActivity.class);
            //                intent.putExtra(Exam2tActivity.EXAMEXAM,exam.getExamId());
            //                intent.putExtra(Exam2tActivity.EXAMCOUNT,String.valueOf(i));
            //                intent.putExtra(Exam2tActivity.EXAMNAME,exam.getName());
            //                mContext.startActivity(intent);
        }
        return holder
    }


    override fun onBindViewHolder(holder: Hour_skycon_tem_adapter.ViewHolder, position: Int) {

        val calendar = Calendar.getInstance()


        calendar.timeZone = TimeZone.getTimeZone("GMT+8:00")

        //小时
        val hour = calendar.get(Calendar.HOUR_OF_DAY)
        val date = calendar.get(Calendar.DATE)


        holder.hour.text = getHour(hour, date, position)
        //holder.hour.setText(String.valueOf(hour+position));

        //holder.time.setText(String.valueOf(position));
        holder.skycon.setImageBitmap(Weather_Dict.skycon_dictm(mweatherApi.result.hourly.skycon[position].value))
        //holder.skycon.setText(Weather_Dict.skycon_dict(mweatherApi.getResult().getDaily().getSkycon().get(position).getValue()));
        holder.tem.text = mweatherApi.result.hourly.temperature[position].value.toString() + "°C"


    }


    override fun getItemCount(): Int {
        return mweatherApi.result.hourly.temperature.size
    }

    fun getHour(h: Int, d: Int, p: Int): String {
        //  KLog.d(String.valueOf(h)+"位置"+String.valueOf(p));
        var s = h + p
        when (s / 24) {
            0 -> return d.toString() + "日" + s + "时"
            1 -> {
                s = s - 24
                return (d + 1).toString() + "日" + s + "时"
            }
            2 -> {
                s = s - 48
                return (d + 2).toString() + "日" + s + "时"
            }
            3 -> {
                s = s - 72
                return (d + 3).toString() + "日" + s + "时"
            }
        }
        return d.toString() + "日\n" + s + "时"

    }

}
